<html xmlns:th="http://www.thymeleaf.org">
<style>
    .layui-form-item .layui-input-inline{
        width: 500px;
    }
</style>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend th:text="${ad == null}?'广告录入':'编辑广告'"></legend>
</fieldset>

<form class="layui-form" action="return false" lay-filter="formUpdate">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">广告图</label>
            <div class="layui-input-inline">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <input type="hidden" name="imgurl" lay-verify="requiredimg"  />
                    <div class="layui-upload-list">
                        <img style="width: 500px; height: 200px; margin: 0 10px 10px 0;" id="demo1" />
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" name="remark" maxlength="100" autocomplete="off" placeholder="请输入备注" class="layui-input" />
            </div>
        </div>
    </div>

    <div class="layui-form-item menu_hide">
        <div class="layui-inline">
            <label class="layui-form-label">排序值</label>
            <div class="layui-input-inline">
                <input type="number" name="order" lay-verify="required|order" autocomplete="off" class="layui-input" value="0" />
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="radio" name="open" value="false" title="关闭" />
                <input type="radio" name="open" value="true" title="开启" checked="true" />
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">有效时间</label>
            <div class="layui-input-inline" style="width: 200px">
                <input type="text" class="layui-input" name="time_range" id="test6" placeholder=" - " style="text-align: center" readonly="readonly"/>(无限期则不填)
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="ad">立即提交</button>
        </div>
    </div>
</form>
<script th:inline="javascript">
    /* <![CDATA[*/
    layui.use(['upload','form', 'layedit', 'laydate','element'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var layedit = layui.layedit;
        var laydate = layui.laydate;
        var element=layui.element;
        var upload = layui.upload;
        var URL = /*[[ ${#httpServletRequest.getContextPath()} ]]*/;
        //日期范围
        laydate.render({
            elem: '#test6'
            ,range: true
            ,trigger: 'click'
        });

        //自定义验证规则
        form.verify({
            order:[/^(\d{1,2}|100)$/,'排序值范围是0-100']
            ,requiredimg:function(value,item){
                if(value==""){
                    return "图片必须上传";
                }
            }
        });

        var ad=/*[[ ${ad} ]]*/;
        var url = URL+'/admin/ads/addAD';
        if(ad){
            var time_range = ""
            if(ad.valid_start_time!=null){
                time_range = new Date(ad.valid_start_time).Format("yyyy-MM-dd")+" - "+new Date(ad.valid_end_time).Format("yyyy-MM-dd");
            }
            form.val("formUpdate", {
                "remark": ad.remark // "name": "value"
                ,"imgurl":ad.imgurl
                ,"open":ad.open+""
                ,"order": ad.order
                ,"time_range":time_range
            })

            $('#demo1').attr('src', URL+ad.imgurl);
            url = URL+'/admin/ads/editAD';
        }
        //监听提交
        form.on('submit(ad)', function(data){
            if(ad){
                data.field.id=ad.id;
            }
            ajaxUtil(url,data.field,function(){
                layer.confirm('操作成功，是否返回列表页面？', {
                    btn: ['确定', '取消']
                }, function(index, layero){
                    layer.close(index);
                    openwindows(URL+'/admin/ads/list',null,$)
                }, function(index){
                    layer.close(index);
                });
            });
            return false;
        });

        var uploadInst = upload.render({
            elem: '#test1'
            ,auto:false
            ,size:2048
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                    $("[name='imgurl']").val(result);
                });
            }
        });
        form.render();

    });
    /* ]]> */
</script>
</html>